import { NodeSize } from '../config'

export default function tableOutput(flow) {
  flow.register('TableOutput', ({ PolygonNode, PolygonNodeModel, h }) => {
    class Node extends PolygonNode {
      getIconShape() {
        return h(
          'svg',
          {
            width: NodeSize,
            height: NodeSize,
            viewBox: '0 0 1024 1024'
          },
          h(
            'path',
            {
              fill: '#3D6480',
              d: 'M131.047619 299.008l155.745524-0.024381V144.725333H240.444952v75.166477L113.103238 92.525714 80.335238 125.269333l127.366095 127.414857H131.047619z'
            }
          ),
          h(
            'path',
            {
              fill: '#EFF9FE',
              d: 'M405.479619 410.916571h19.504762v0.219429h180.711619v-0.219429h19.504762v0.219429h159.207619v-159.402667H424.277333l-159.402666 159.402667h140.604952z'
            }
          ),
          h(
            'path',
            {
              fill: '#3D6480',
              d: 'M465.700571 210.310095l-41.423238 41.423238h360.155429v159.402667h-159.207619v-0.219429h-19.504762v0.219429h-180.711619v-0.219429h-19.504762v0.219429h-140.629333l-19.529143 19.529143h160.158476v180.687238H240.737524v-176.079238l-41.447619 41.447619v360.155428h626.590476V210.310095H465.700571z m-60.220952 585.142857H240.737524v-164.571428h164.766476v164.571428z m200.216381 0h-180.711619v-164.571428h180.711619v164.571428z m0-184.100571h-180.711619v-180.687238h180.711619v180.687238z m178.712381 184.100571h-159.207619v-164.571428h159.207619v164.571428z m0-184.100571h-159.207619v-180.687238h159.207619v180.687238z'
            }
          )
        )
      }

      getLabelShape() {
        return h(
          'text',
          {
            fill: '#000000',
            fontSize: 12,
            x: NodeSize / 2,
            y: NodeSize + 20,
            textAnchor: 'middle',
            dominantBaseline: 'middle'
          },
          '表输出'
        )
      }

      getShape() {
        const { x, y, width, height } = this.getAttributes()
        const transform = `matrix(1 0 0 1 ${x - width / 2} ${y - height / 2})`
        return h(
          'g',
          {
            transform
          },
          [
            h(
              'rect',
              {
                rx: 5,
                ry: 5,
                width: NodeSize,
                height: NodeSize,
                fill: '#fff',
                stroke: '#3D6380',
                strokeWidth: 1
              }
            ),
            this.getIconShape(),
            this.getLabelShape()
          ]
        )
      }
    }
    class Model extends PolygonNodeModel {
      constructor(data, graphModel) {
        super(data, graphModel)
        this.points = [
          [NodeSize / 2, 0],
          [NodeSize, NodeSize / 2],
          [NodeSize / 2, NodeSize],
          [0, NodeSize / 2]
        ]
      }
    }

    return {
      view: Node,
      model: Model
    }
  })
}
